<template>
	<view class="container">
		<uni-notice-bar v-if="noticeList.length > 0" v-for="(item, index) in noticeList" :key="index" :text="item.data"
			scrollable="true"></uni-notice-bar>
		<!-- 顶部标题 -->
		<view class="header">
			<text class="title">新干铁匠铺</text>
		</view>

		<!-- 用户信息 -->
		<view class="user-info" @tap="showLogoutConfirm">
			<image class="user-avatar" :src="avatar || '/static/avatar/empty.png'"></image>
			<text class="user-name">{{c_name}}</text>
		</view>

		<!-- 权限模块 -->
		<PermissionSection v-if="role == 99" title="超级管理区" :items="superAdmin" @item-click="navigateTo" />
		<PermissionSection v-if="role >= 5" title="管理区" :items="management" @item-click="navigateTo" />
		<PermissionSection v-if="role != 2" title="功能区" :items="functional" @item-click="navigateTo" />
		<PermissionSection v-if="role == 1 || role > 90" title="司机专区" :items="driver" @item-click="navigateTo" />
		<PermissionSection v-if="role == 2 || role > 90" title="供应商专区" :items="supplier" @item-click="navigateTo" />
	</view>
</template>

<script>
	import PermissionSection from '@/components/PermissionSection.vue'

	export default {
		components: {
			PermissionSection
		},
		data() {
			return {
				noticeList: [],
				role: -1,
				c_name: '',
				avatar: '',
				superAdmin: [{
						name: '组织架构',
						icon: 'sitemap',
						url: '/pagesA/admin/organization/list'
					},
					{
						name: '职务管理',
						icon: 'id-badge',
						url: '/pagesA/admin/job/list'
					},
					{
						name: '权限管理',
						icon: 'lock',
						url: '/pagesA/admin/power/list'
					}
				],
				management: [{
						name: '公示公告',
						icon: 'bullhorn',
						url: '/pagesA/notice/list'
					},
					{
						name: '用车管理',
						icon: 'car',
						url: '/pagesA/car/schedules_list'
					},
					{
						name: '用餐管理',
						icon: 'spoon',
						url: '/pagesA/meal/schedules_list'
					},
					{
						name: '用户管理',
						icon: 'users',
						url: '/pagesA/user/list'
					},
					{
						name: '司机管理',
						icon: 'drivers-license',
						url: '/pagesA/driver/list'
					},
					{
						name: '值班管理',
						icon: 'calendar-alt',
						url: '/pagesA/duty/duty-swiper'
					},
					{
						name: '物品管理',
						icon: 'box-archive',
						url: '/pagesA/item/item'
					},
					{
						name: '签到管理',
						icon: 'clipboard',
						url: '/pagesA/check/list'

					}
				],
				functional: [{
						name: '出行预约',
						icon: 'car-alt',
						url: '/pagesB/car/schedules'
					},
					{
						name: '工作餐',
						icon: 'bell-concierge',
						url: '/pagesB/meal/schedules'
					},
					{
						name: '物品领用',
						icon: 'box-open',
						url: '/pagesB/item/item'
					},
					{
						name: '值班排班',
						icon: 'calendar-check',
						url: '/pagesB/duty/duty-swiper'
					},
					{
						name: '签到打卡',
						icon: 'clipboard-check',
						url: '/pagesB/checkin/list'
					}
				],
				driver: [{
						name: '我的任务',
						icon: 'tasks',
						url: '/pagesC/driver/mission'
					},
					{
						name: '加油记录',
						icon: 'charging-station',
						url: '/pagesC/driver/fuel/list'
					}
				],
				supplier: [{
					name: '我的订单',
					icon: 'shopping-cart',
					url: '/pagesC/supplier/order'
				}]
			}
		},
		onShow() {
			const isLogin = uni.getStorageSync('isLogin')
			if (isLogin) {
				const userinfo = uni.getStorageSync('userInfo')
				this.c_name = userinfo.username
				this.avatar = userinfo.avatar
				this.role = userinfo.role?.value ?? -1
			} else {
				this.c_name = '未登录'
			}
			this.getNoticeList()
			// #ifdef MP-WEIXIN
			this.setupWeixinShare()
			// #endif
		},
		methods: {
			// ============= 新增微信分享方法 =============
			setupWeixinShare() {
				// 开启转发功能
				uni.showShareMenu({
					withShareTicket: true,
					menus: ['shareAppMessage', 'shareTimeline']
				})
			},

			// 分享给朋友
			onShareAppMessage() {
				return {
					title: '新干铁匠铺 - 便捷办公服务平台',
					path: '/pages/index/index',
					imageUrl: 'https://example.com/static/share.jpg', // 替换为实际分享图片URL
					success: (res) => {
						uni.showToast({
							title: '分享成功',
							icon: 'success'
						})
					},
					fail: (err) => {
						console.error('分享失败:', err)
					}
				}
			},

			// 分享到朋友圈
			onShareTimeline() {
				return {
					title: '推荐使用新干铁匠铺办公平台',
					path: '/pages/index/index',
					imageUrl: 'https://example.com/static/share.jpg', // 替换为实际分享图片URL
					success: (res) => {
						uni.showToast({
							title: '分享成功',
							icon: 'success'
						})
					},
					fail: (err) => {
						console.error('分享失败:', err)
					}
				}
			},

			navigateTo(url) {
				if (url) uni.$router.to(url)
			},
			getNoticeList() {
				const obj = uniCloud.importObject('notice')
				const res = obj.ActivelistNotices().then(r => {
					if (r.code === 0) {
						this.noticeList = r.data
						// console.log(this.noticeList)
					}
				})
			},
			showLogoutConfirm() {
				if (!uni.getStorageSync('isLogin')) {
					uni.showModal({
						title: '提示',
						content: '尚未登录，要登录吗？',
						success: (res) => {
							if (res.confirm) {
								// 跳转到登录页（请根据你的实际路径修改）
								uni.reLaunch({
									url: '/pages/login/login'
								})
							}
						}
					})
				} else {
					uni.showModal({
						title: '提示',
						content: '确定要退出登录吗？',
						success: (res) => {
							if (res.confirm) {
								// 清除登录状态
								uni.removeStorageSync('isLogin')
								uni.removeStorageSync('userInfo')

								// 跳转到登录页（请根据你的实际路径修改）
								uni.reLaunch({
									url: '/pages/index/index'
								})
							}
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	/* 全局样式 */
	* {
		margin: 0;
		padding: 0;
		box-sizing: border-box;
		font-family: 'Helvetica Neue', Arial, sans-serif;
	}

	.container {
		max-width: 800rpx;
		margin: 0 auto;
		padding-bottom: 40rpx;
		background: linear-gradient(135deg, #f0f8ff 0%, #e6f7ff 100%);
	}

	.header {
		text-align: center;
		padding: 25rpx 0;
		margin-bottom: 10rpx;
	}

	.title {
		font-size: 40rpx;
		color: #1a73e8;
		font-weight: 600;
		position: relative;
		display: inline-block;
	}

	.title::after {
		content: '';
		position: absolute;
		bottom: -8rpx;
		left: 50%;
		transform: translateX(-50%);
		width: 80rpx;
		height: 4rpx;
		background: linear-gradient(90deg, #1a73e8, #64b5f6);
		border-radius: 2rpx;
	}

	/* 用户信息样式 */
	.user-info {
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-bottom: 30rpx;
	}

	.user-avatar {
		width: 100rpx;
		height: 100rpx;
		border-radius: 50%;
		border: 3rpx solid #1a73e8;
		box-shadow: 0 4rpx 10rpx rgba(0, 0, 0, 0.1);
	}

	.user-name {
		margin-top: 10rpx;
		font-size: 24rpx;
		color: #333;
		font-weight: 500;
	}
</style>